//
//  ChangeImageController.m
//  NoNameAmazing
//
//  Created by liuyang on 2017/9/27.
//  Copyright © 2017年 北京执行力科技有限公司. All rights reserved.
//

#import "ChangeImageController.h"

@interface ChangeImageController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *headImage;
@property (weak, nonatomic) IBOutlet UIButton *xiangceBtn;
@property (weak, nonatomic) IBOutlet UIButton *paizhaoBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *headHigh;
@property(nonatomic,strong)UIImage * image;
@end

@implementation ChangeImageController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.headHigh.constant = kScreen_Width;
    [self.headImage sd_setImageWithURL:self.url];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonTitle:@"保存" image:nil frame:CGRectMake(0, 0, 30, 20) target:self sel:@selector(baoClick)];
}
- (IBAction)xiangceCLICK:(id)sender {
    UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
    //获取方式1：通过相册（呈现全部相册），UIImagePickerControllerSourceTypePhotoLibrary
    //获取方式2，通过相机，UIImagePickerControllerSourceTypeCamera
    //获取方法3，通过相册（呈现全部图片），UIImagePickerControllerSourceTypeSavedPhotosAlbum
    PickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    //允许编辑，即放大裁剪
    PickerImage.allowsEditing = YES;
    //自代理
    PickerImage.delegate = self;
    //页面跳转
    [self presentViewController:PickerImage animated:YES completion:nil];
    
}

- (IBAction)paizhaoClick:(id)sender {
    /**
     其实和从相册选择一样，只是获取方式不同，前面是通过相册，而现在，我们要通过相机的方式
     */
    UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
    //获取方式:通过相机
    PickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;
    PickerImage.allowsEditing = YES;
    PickerImage.delegate = self;
    [self presentViewController:PickerImage animated:YES completion:nil];

}

//PickerImage完成后的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    //定义一个newPhoto，用来存放我们选择的图片。
    UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    _image = newPhoto;
    _headImage.image = newPhoto;
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)baoClick
{
    // 1.创建网络管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 2.拼接请求参数url 也可在具体控制器请求里传入
    NSDictionary *dict = @{@"userid" : [us objectForKey:kUserid]};
    // 3.发送请求
    [manager POST:[NSString stringWithFormat:@"%@%@",[us objectForKey:baseUrl],@"/informationsite/uploadTitleImage.do"] parameters:dict constructingBodyWithBlock:
     ^void(id<AFMultipartFormData> formData) {
         
         NSData *imageData = UIImageJPEGRepresentation(_image, 0.5);//进行图片压缩
         
         // 使用日期生成图片名称
         NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
         formatter.dateFormat = @"yyyyMMddHHmmss";
         NSString *fileName = [NSString stringWithFormat:@"%@.png",[formatter stringFromDate:[NSDate date]]];
         // 任意的二进制数据MIMEType application/octet-stream
         [formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/png"];
         
     } success:^void(NSURLSessionDataTask * task, id responseObject) {
         NSLog(@"%@",responseObject);
         if ([responseObject[@"status"] integerValue]==1) {
             //创建字典
             NSString * BaseURL = [us objectForKey:baseUrl];
             NSDictionary *dict = @{@"image":[NSString stringWithFormat:@"%@%@",BaseURL,responseObject[@"imgUrl"]]
                                              };
             //发送通知
             [[NSNotificationCenter defaultCenter]postNotificationName:@"head" object:nil userInfo:dict];
             [self.navigationController popViewControllerAnimated:YES];
         }

         
     } failure:^void(NSURLSessionDataTask * task, NSError * error) {
         
         
     }];
    
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
